/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.jr.wx.video.downloadagent.common;

import cn.ac.iie.di.ban.jr.wx.video.downloadagent.config.ConfigureUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;


import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.log4j.Logger;


/**
 *
 * @author QJP
 */
public class GlobalParas {

    public static Logger LOG = Logger.getLogger(GlobalParas.class);

    //properties name
    public final static String LOG4_CONFIGURATION = "log4j.properties";
    public final static String DEFAULT_CONFIGURATION = "config.properties";
    public final static String CONSUME_FROM_FIRST_OFFSET = "first";
    public final static String CONSUME_FROM_LAST_OFFSET = "last";
    public final static String CONSUME_FROM_TIMESTAMP = "timestamp";
    public final static Integer QUEUE_SIZE = 20;


    //configuration.properties parameters

    public static String CONSUMER_NAMESERVER = "";
    public static List<Object> CONSUMER_TOPIC_GROUP;
    public static String CONSUMER_POSITION = "";
    public static String CONSUMER_TIMESTAMPS = "";
    public static String CONSUMER_DATEFORMAT = "";

    public static Boolean DATA_FIND;
    public static String DATA_COLUMN;
    public static List<Object> DATA_VALUES;

    public static List<String> TOPIC_LIST;
    public static HashMap<String, String> GROUP_MAP;

    private static volatile GlobalParas instanceDefConf;

    public GlobalParas(String confPropertiesName) throws ConfigurationException {
        String confPropertiesPath = ClassLoader.getSystemClassLoader().getResource(confPropertiesName).getFile();
        ConfigureUtil configureUtil = new ConfigureUtil();
        FileBasedConfiguration configuration = configureUtil.initConfiguration(confPropertiesPath);
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            initConf(configuration);
        }
    }

    public static GlobalParas getIstance(String confPropertiesName) throws ConfigurationException {
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            if (instanceDefConf == null) {
                synchronized (GlobalParas.class) {
                    if (instanceDefConf == null) {
                        instanceDefConf = new GlobalParas(confPropertiesName);
                    }
                }
            }
            return instanceDefConf;
        }
        else {
            LOG.error("Could Not Find Configuration: " + confPropertiesName);
            return null;
        }
    }

    public static void initConf(FileBasedConfiguration configuration) {

        TOPIC_LIST = new ArrayList<>();

        CONSUMER_NAMESERVER = configuration.getString("consumer.nameserver");
        CONSUMER_TOPIC_GROUP = configuration.getList("consumer.topic.group");
        CONSUMER_POSITION = configuration.getString("consumer.position");
        CONSUMER_TIMESTAMPS = configuration.getString("consumer.timestamps");
        CONSUMER_DATEFORMAT = configuration.getString("consumer.dateformat");

        DATA_FIND = configuration.getBoolean("data.find");
        DATA_COLUMN = configuration.getString("data.column");
        DATA_VALUES = configuration.getList("data.values");

        GROUP_MAP = initGroupMap();


    }

    private static HashMap<String, String> initGroupMap() {
        HashMap<String, String> hashMap = new HashMap<>();
        for (Object topicGroup: CONSUMER_TOPIC_GROUP) {
            hashMap.put(String.valueOf(topicGroup).split(":")[0], "iie_di_data_check_" + String.valueOf(topicGroup).split(":")[1] + "_" + String.valueOf(topicGroup).split(":")[0]);
            TOPIC_LIST.add(String.valueOf(topicGroup).split(":")[0]);
        }
        return hashMap;
    }


}
